package com.guli.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.result.Result;
import com.guli.teacher.entity.EduCourse;
import com.guli.teacher.entity.EduCourseDescription;
import com.guli.teacher.entity.EduTeacher;
import com.guli.teacher.entity.EduVideo;
import com.guli.teacher.entity.query.CourseQuery;
import com.guli.teacher.entity.vo.CoursePublishVo;
import com.guli.teacher.entity.vo.CourseVo;
import com.guli.teacher.entity.vo.CourseWebVo;
import com.guli.teacher.exception.EduException;
import com.guli.teacher.mapper.EduCourseMapper;
import com.guli.teacher.service.EduChapterService;
import com.guli.teacher.service.EduCourseDescriptionService;
import com.guli.teacher.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.teacher.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author guli
 * @since 2020-05-29
 */
@Service
@Transactional
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Override
    public String saveVO(CourseVo vo) {

        // 1 添加课程
        baseMapper.insert(vo.getEduCourse());
        // 2 获取课程Id
        String courseId = vo.getEduCourse().getId();
        // 3 添加课程描述
        vo.getCourseDescription().setId(courseId);
        courseDescriptionService.save(vo.getCourseDescription());

        return courseId;
    }

    @Override
    public CourseVo getCourseVoById(String id) {

        // 创建一个Vo对象
        CourseVo vo = new CourseVo();
        // 根据课程Id获取课程对象
        EduCourse eduCourse = baseMapper.selectById(id);
        if (eduCourse == null) {
            return vo;
        }
        // 把课程加入到Vo对象中
        vo.setEduCourse(eduCourse);
        // 根据课程Id获取出课程描述
        EduCourseDescription eduCourseDescription = courseDescriptionService.getById(id);
        // 把描述加在vo对象中
        if (eduCourseDescription == null) {
            return vo;
        }
        vo.setCourseDescription(eduCourseDescription);

        return vo;
    }

    /**
     * 修改course
     *
     * @param vo
     * @return
     */
    @Override
    public Boolean updateVo(CourseVo vo) {

        if (StringUtils.isEmpty(vo.getEduCourse().getId())) {
            throw new EduException(20001, "没有课程编号，修改失败");
        }
        int i = baseMapper.updateById(vo.getEduCourse());
        if (i <= 0) {
            throw new EduException(20001, "修改课程信息失败");
        }
        //获取course—ID
        String courseId = vo.getEduCourse().getId();
        //设置课程描述的ID
        vo.getCourseDescription().setId(courseId);
        boolean b = courseDescriptionService.updateById(vo.getCourseDescription());
        return b;

    }

    /**
     * 分页查询
     *
     * @param objectPage
     * @param courseQuery
     */
    @Override
    public void getPageList(Page<EduCourse> objectPage, CourseQuery courseQuery) {

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        if (courseQuery == null) {
            baseMapper.selectPage(objectPage, wrapper);
        }
        String subjectId = courseQuery.getSubjectId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String teacherId = courseQuery.getTeacherId();
        String title = courseQuery.getTitle();

        if (!StringUtils.isEmpty(subjectId)) {
            wrapper.eq("subject_id", subjectId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            wrapper.eq("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(teacherId)) {
            wrapper.eq("teacher_id", teacherId);
        }
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }

        baseMapper.selectPage(objectPage, wrapper);

    }


    @Autowired
    private EduVideoService videoService;
    @Autowired
    private EduChapterService chapterService;
    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(String id) {
        // TODO 删除课程相关的小节
        //根据课程ID删除小节
        videoService.removeVideoByCourseId(id);
        // TODO 删除课程相关的章节
        chapterService.removeByCourseId(id);
        // 删除描述
        boolean b = courseDescriptionService.removeById(id);
        if (!b) {
            return false;
        }
        // 删除基本信息
        int i = baseMapper.deleteById(id);
        return i == 1;
    }

    @Override
    public CoursePublishVo getCoursePublishVoById(String id) {
        CoursePublishVo vo = baseMapper.getCoursePublishVoById(id);
        return vo;
    }

    @Override
    public Boolean updateStatusById(String id) {

        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus("Normal");
        int i = baseMapper.updateById(course);
        return i == 1;

    }

    @Override
    public Map<String, Object> getMapById(String id) {

        Map<String, Object> map = baseMapper.getMapById(id);

        return map;
    }

    /**
     * 根据讲师id查询讲师所讲课程
     *
     * @param id
     * @return
     */
    @Override
    public List<EduCourse> getCouseListTeacherId(String id) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id",id);

        // 根据最后更新时间降序排列
        wrapper.orderByDesc("gmt_modified");

        List<EduCourse> eduCourses = baseMapper.selectList(wrapper);
        return eduCourses;
    }

    /**
     * 课程分页
     *
     * @param pageCourse
     * @return
     */
    @Override
    public Map<String, Object> getFrontCourseList(Page<EduCourse> pageCourse) {

        baseMapper.selectPage(pageCourse,null);
        long current = pageCourse.getCurrent(); // 当前页
        long pages = pageCourse.getPages(); // 总页数
        long size = pageCourse.getSize(); // 每页显示记录数
        long total = pageCourse.getTotal(); // 总记录数
        List<EduCourse> records = pageCourse.getRecords(); // 每页数据的list集合
        boolean hasPrevious = pageCourse.hasPrevious(); // 是否有上一页
        boolean hasNext = pageCourse.hasNext();// 是否有下一页

        // 封装map
        Map<String,Object> map = new HashMap<>();

        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    /**
     * 根据课程id查询课程基本信息
     * @param id
     * @return
     */
    @Override
    public CourseWebVo getCourseInfoFrontId(String id) {

        CourseWebVo courseWebVo = baseMapper.getFrontCourseInfoId(id);

        return courseWebVo;
    }


}